package com.general.app.wjgl.action;

import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.general.app.wjgl.dao.entity.Question;
import com.general.app.wjgl.dao.entity.Survey;
import com.general.app.wjgl.service.IQuestionService;
import com.general.app.wjgl.service.ISurveyService;
import com.general.common.action.BaseAction;
import com.general.common.exception.BusinessException;
import com.general.common.model.QueryModel;
import com.general.common.security.entity.User;
import com.general.utils.base.Constant;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.util.ValueStack;

/**
 * 问卷管理
 * @author Administrator
 *
 */
public class SurveyAction extends BaseAction implements ModelDriven<Survey> {

	private static final long serialVersionUID = 1L;
	
	private Survey model = new Survey();
	private ISurveyService surveyService;
	private IQuestionService questionService;
	
	
	public Survey getModel() {
		return model;
	}


	public void setSurveyService(ISurveyService surveyService) {
		this.surveyService = surveyService;
	}

	
	public void setQuestionService(IQuestionService questionService) {
		this.questionService = questionService;
	}


	public String surveyList(){
		
		return "surveyList";
	}
	
	
	/**
	 * 查询问卷列表
	 * @return
	 */
	public String getSurveyList(){
		
		QueryModel queryModel = model.getQueryModel();
		List<Survey> surveyList = surveyService.getPagedList(model);
		queryModel.setItems(surveyList);
		ValueStack vs = getValueStack();
		vs.set(DATA, queryModel);
		
		return DATA;
	}
	
	public String toAddSurvey(){
		
		return "addSurvey";
	}
	
	/**
	 * 保存问卷
	 * @return
	 */
	public String addSurvey(){
		
		User user = getCurrentUser();
		model.setCreateUser(user.getUsername());
		
		boolean result = surveyService.insert(model);
		String key = result ? Constant.SAVE_SUCCEE : Constant.SAVE_FAIL;
		getValueStack().set(DATA, getText(key));
		
		return DATA;
	}
	
	
	/**
	 * 访问修改页面
	 * @return
	 */
	public String toUpdateSurvey(){
		
		String id = model.getId();
		Survey survey = surveyService.getModel(id);
		
		try {
			BeanUtils.copyProperties(model, survey);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		} 
		
		return "updateSurvey";
	}
	
	
	/**
	 * 修改问卷
	 * @return
	 */
	public String updateSurvey(){
		
		boolean result = surveyService.update(model);
		String key = result ? Constant.SAVE_SUCCEE : Constant.SAVE_FAIL;
		getValueStack().set(DATA, getText(key));
		
		return DATA;
	}
	
	
	/**
	 * 删除问卷
	 * @return
	 */
	public String deleteSurvey(){
		String ids = getRequest().getParameter("ids");
		try {
			
			int delCount = surveyService.delete(ids);
			getValueStack().set(DATA, getText(Constant.DELETE_INFO,new String[]{"调查问卷",String.valueOf(delCount)}));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return DATA;
	}


	/**
	 * 访问设计问卷页面
	 * @return
	 */
	public String toDeginSurvey(){
		String id = model.getId();
		Survey survey = surveyService.getModel(id);
		
		try {
			BeanUtils.copyProperties(model, survey);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		} 
		return "deginSurvey";
	}

	
	/**
	 * 保存问卷设计
	 * @return
	 */
	public String saveDeginSurvey(){
		
		String surveyInfo = getRequest().getParameter("jsonData");
		String key = null;
		try {
			boolean result = surveyService.saveSurveyDegin(model,surveyInfo);
			key = result ? Constant.SAVE_SUCCEE : Constant.SAVE_FAIL;
			getValueStack().set(DATA, getText(key));
		} catch (BusinessException e) {
			e.printStackTrace();
			getValueStack().set(DATA, getText(Constant.SAVE_FAIL));
		}
		return DATA;
	}
	
	
	
	/**
	 * 问卷预览
	 * @return
	 */
	public String viewSurvey(){
		
		try {
			Survey survey = surveyService.getModel(model.getId());
			BeanUtils.copyProperties(model, survey);
			List<Question> questionList = questionService.getSurveyQuestionList(model.getId());
			getValueStack().set("questionList", questionList);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		} 
		
		return "viewSurvey";
	}
	
	/**
	 * 复制问卷
	 * @return
	 */
	public String copySurvey(){
		String surveyId = getRequest().getParameter("id");
		String key = null;
		try {
			boolean result = surveyService.copySurvey(surveyId);
			key = result ? "复制成功" : "复制失败";
			getValueStack().set(DATA, getText(key));
		} catch (Exception e) {
			e.printStackTrace();
			getValueStack().set(DATA, "复制失败");
		}
		
		return DATA;
	}
	
}
